self.importScripts("./spark-md5.min.js")


self.onmessage = (e) => {
    const { chunks } = e.data;
    const spark = new SparkMD5.ArrayBuffer();
    const fileReader = new FileReader();
    let length = chunks.length;
    function loadNext(index) {//加载下一个分片
        fileReader.readAsArrayBuffer(chunks[index].fileChunk);
        fileReader.onload = function (e) {
            spark.append(e.target.result);
            index++;
            if (chunks[index] && chunks[index].fileChunk) {
                self.postMessage({
                    progress: index / length
                })
                loadNext(index);
            } else {
                var hash = spark.end();
                self.postMessage({
                    hash
                })
            }
        };
    }
    loadNext(0)
}